//============================================================================
// Name        : slide_effect.cpp
// Author      : dfoelber
//============================================================================

// INCLUDES //
#include "effect/slide_effect.h"
#include <stdlib.h>
//////////////

/**
* Constructor
* 
* @param owner The owner to this effect
**/
SlideEffect::SlideEffect(RegionEffectOwner* owner) :
	PuzzleEffect(owner)
{
	selectedObject = NULL;
	oldX = oldY = selectedX = selectedY = selectedWidth = selectedHeight = -1;
}

/**
* Virtual destructor
**/
SlideEffect::~SlideEffect()
{
}

/**
* Notifies the effect to start.
* 
* Overridden from RegionEffect.
**/
void SlideEffect::OnStart()
{
}

/**
* Notifies the effect that the finish state of the puzzle region is finished.
* 
* Overridden from PuzzleEffect.
**/
void SlideEffect::OnFinishCompleted()
{
}

/**
* Asks the effect if it wants to freeze the active object and its association list.
* 
* Overridden from RegionEffect.
* 
* @return If the active object and association are to be frozen
**/
bool SlideEffect::FreezeActiveAndSimilar()
{
	return false;
}

/**
* Input that can be used by the effect.
* 
* @param event The event
* @return If the event was consumed
**/
bool SlideEffect::Input( SDL_Event event )
{
	if( event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT )
	{
		Object* object = GetOwner()->GetObjectByCoordinate( event.button.x, event.button.y );
		
		if( object != NULL && !GetOwner()->IsObjectActiveOrSimilar(object) )
		{
			selectedX = object->GetPath()->GetX();
			selectedY = object->GetPath()->GetY();
			selectedWidth = object->GetPath()->GetWidth();
			selectedHeight = object->GetPath()->GetHeight();
			selectedObject = object;
			oldX = event.button.x;
			oldY = event.button.y;
		}
		
		return true;
	}
	else if( event.type == SDL_MOUSEBUTTONUP )
	{
		selectedObject = NULL;
		oldX = oldY = selectedX = selectedY = selectedWidth = selectedHeight = -1;
	}
	else if( event.type == SDL_MOUSEMOTION && selectedObject != NULL && !selectedObject->GetPath()->IsMoving() )
	{
		if( event.button.x > selectedObject->GetPath()->GetX() + selectedObject->GetPath()->GetWidth() + 1 )
		{
			GetOwner()->TranslateObject( selectedObject, selectedObject->GetPath()->GetWidth() + 1, 0 );
		}
		else if( event.button.x < selectedObject->GetPath()->GetX() - 1 )
		{
			GetOwner()->TranslateObject( selectedObject, -(selectedObject->GetPath()->GetWidth() + 1), 0 );
		}
		oldX = event.button.x;
		oldY = event.button.y;
	}
	
	return false;
}

/**
* Notifies that the TimeRegion's countdown is complete.
* 
* Overridden from TimeRegionListener
**/
void SlideEffect::CountdownFinished()
{
	GetOwner()->EffectDone();
}
